package com.guigu.qingcheng.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbPref;
import com.guigu.qingcheng.servicegoods.mapper.TbPrefMapper;
import com.guigu.qingcheng.servicegoods.service.TbPrefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbPrefServiceImpl extends ServiceImpl<TbPrefMapper, TbPref> implements TbPrefService {

    @Resource
    private TbPrefMapper mapper;

    @Autowired
    private TbPrefService tbPrefService;

    //根据条件查询封装查询对象
    public QueryWrapper<TbPref> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbPref> wapper = new QueryWrapper<TbPref>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("cateId") != null && !"".equals(searchMap.get("cateId")))
                wapper.eq("cate_id", searchMap.get("cateId"));

            if(searchMap.get("buyMoney") != null && !"".equals(searchMap.get("buyMoney")))
                wapper.eq("buy_money", searchMap.get("buyMoney"));
            if(searchMap.get("preMoney") != null && !"".equals(searchMap.get("preMoney")))
                wapper.eq("pre_money", searchMap.get("preMoney"));

            if(searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")))
                wapper.ge("start_time", searchMap.get("startTime"));
            if(searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime")))
                wapper.le("end_time", searchMap.get("endTime"));

            if(searchMap.get("type") != null && !"".equals(searchMap.get("type")))
                wapper.eq("type", searchMap.get("type"));
            if(searchMap.get("state") != null && !"".equals(searchMap.get("state")))
                wapper.eq("state", searchMap.get("state"));
        }
        return wapper;
    }

    @Override
    public Page<TbPref> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<TbPref> pageTbPref = new Page<TbPref>(page, size);
        QueryWrapper<TbPref> query = queryWrapper(searchMap);
        IPage<TbPref> list = this.tbPrefService.page(pageTbPref, query);
        return pageTbPref;
    }

    @Override
    public List<TbPref> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbPref> wrapper = queryWrapper(searchMap);
        List<TbPref> list = this.tbPrefService.list(wrapper);
        return list;
    }
}
